from socket import *

# 1. 创建套接字，注意，参数二需改为SOCK_DGRAM
sk = socket(AF_INET, SOCK_DGRAM)

# 2. 绑定服务地址
server_addr = ('127.0.0.1', 8080)
sk.bind(server_addr)

# 3. 收发消息，接收所有人的消息
while True:
    try:
        data, addr = sk.recvfrom(1024)
        print('recv<<<', data.decode())

        # 回复消息到发送者
        send_msg = '收到来自%s消息>>>' % ''.join(str(item) for item in addr)
        sk.sendto(send_msg.encode(), addr)
    except Exception as e:
        print(e)
        break

# 4.关闭连接
sk.close()
